

package com.hazelcast.client.impl.proxy.txn;

import com.hazelcast.client.impl.protocol.ClientMessage;
import com.hazelcast.client.impl.protocol.codec.*;
import com.hazelcast.client.impl.spi.ClientTransactionContext;
import com.hazelcast.internal.serialization.Data;
import com.hazelcast.multimap.impl.MultiMapService;
import com.hazelcast.spi.impl.UnmodifiableLazyList;
import com.hazelcast.transaction.TransactionException;
import com.hazelcast.transaction.TransactionalMultiMap;

import java.util.Collection;
import java.util.List;

import static com.hazelcast.internal.util.ThreadUtil.getThreadId;

/**
 * Proxy implementation of {@link TransactionalMultiMap}
 *
 * @param <K> key
 * @param <V> value
 */
public class ClientTxnMultiMapProxy<K, V> extends ClientTxnProxy implements TransactionalMultiMap<K, V> {

    public ClientTxnMultiMapProxy(String name, ClientTransactionContext transactionContext) {
        super(name, transactionContext);
    }

    @Override
    public boolean put(K key, V value) throws TransactionException {
        ClientMessage request = TransactionalMultiMapPutCodec.encodeRequest(name, getTransactionId(), getThreadId(), toData(key), toData(value));
        ClientMessage response = invoke(request);
        return TransactionalMultiMapPutCodec.decodeResponse(response);
    }

    @Override
    public Collection<V> get(K key) {
        ClientMessage request = TransactionalMultiMapGetCodec.encodeRequest(name, getTransactionId(), getThreadId(), toData(key));

        ClientMessage response = invoke(request);
        List<Data> collection = TransactionalMultiMapGetCodec.decodeResponse(response);
        return new UnmodifiableLazyList(collection, getSerializationService());
    }

    @Override
    public boolean remove(Object key, Object value) {
        ClientMessage request = TransactionalMultiMapRemoveEntryCodec.encodeRequest(name, getTransactionId(), getThreadId(), toData(key), toData(value));
        ClientMessage response = invoke(request);
        return TransactionalMultiMapRemoveEntryCodec.decodeResponse(response);
    }

    @Override
    public Collection<V> remove(Object key) {
        ClientMessage request = TransactionalMultiMapRemoveCodec.encodeRequest(name, getTransactionId(), getThreadId(), toData(key));
        ClientMessage response = invoke(request);
        List<Data> collection = TransactionalMultiMapRemoveCodec.decodeResponse(response);
        return new UnmodifiableLazyList(collection, getSerializationService());
    }

    @Override
    public int valueCount(K key) {
        ClientMessage request = TransactionalMultiMapValueCountCodec.encodeRequest(name, getTransactionId(), getThreadId(), toData(key));
        ClientMessage response = invoke(request);
        return TransactionalMultiMapValueCountCodec.decodeResponse(response);
    }

    @Override
    public int size() {
        ClientMessage request = TransactionalMultiMapSizeCodec.encodeRequest(name, getTransactionId(), getThreadId());
        ClientMessage response = invoke(request);
        return TransactionalMultiMapSizeCodec.decodeResponse(response);
    }

    @Override
    public String getServiceName() {
        return MultiMapService.SERVICE_NAME;
    }

    @Override
    void onDestroy() {
    }
}
